<script>
import findPerson from '../../../publish/components/findPerson.vue';
import Header from "@/components/Header.vue"
export default {
	components: {
		findPerson,
		Header
	},
	onLoad(options) {
		if (options.id) this.id = options.id
		this.getDetail();
		this.list()
	},
	data() {
		return {
			title: "编辑信息",
			id: "",
			item: {},
			dataArr: {},
			imgstr: '',
			str:'',
			loading:true
		}
	},
	methods: {
		getDetail() {
			var that = this
			uni.request({
				url: getApp().globalData.host + '/api/Order/editMyPublish',
				data: {
					edit_type: "1",
					seek_id: that.id
				},
				header: {
					memberid: uni.getStorageSync('member_id'),
					membertoken: uni.getStorageSync('member_token'),
				},
				success: (r) => {
					that.item = r.data.data
					console.log(that.item)
				},
			})
		},
		list() {
			var that = this
			uni.request({
				url: getApp().globalData.host + '/api/My/getSelectList',
				header: {
					memberid: uni.getStorageSync('member_id'),
					membertoken: uni.getStorageSync('member_token'),
				},
				success: (r) => {
					that.dataArr = r.data.data
					that.imageUrl = r.data.data.carousel_list[0].carousel_img
					that.flag = true
					console.log(that.dataArr)
				},
			})
		
		},
		
		listUpdate(e){
			var that = this
			console.log(e)
			that.$nextTick(() => {
				if(e.type=='age'){
					if (e.value < 0) {
						var age = 0; // 最小值限制
					} else if (e.value > 100) {
						var age = 100; // 最大值限制
					} else {
						var age = e.value; // 正常赋值
					}  
					
					that.$set(that.item,"age",age)
					that.$set(that.item,"rand",Math.random())

				}
				if(e.type=='name'){
					const regex = /^[\u4e00-\u9fa5]{0,4}$/;
					if (!regex.test(e.value)) {
						// 如果不匹配，则重置为之前的值或截取前4个字符
						var name = e.value.slice(0, 4);
					} else {
						var name = e.value
					}
					
					that.$set(that.item,"name",name)
					that.$set(that.item,"rand",Math.random())
					
				}
				
			})
			
		},
		
		uploadImages(path) {
			var that = this
			if(path){
				if (path.includes('tmp')) {
					uni.uploadFile({
						url: getApp().globalData.host + '/api/Login/uploadImages',
						filePath: path,
						name: 'image',
						header: {
							memberid: uni.getStorageSync('member_id'),
							membertoken: uni.getStorageSync('member_token'),
						},
						success: res => {
							var json = JSON.parse(res.data);
							that.imgstr += json.data.image_url + ','
						}
					})
				} else {
					that.imgstr += path + ','
				}
			}
		},
		submit(sub) {
			var that = this

			that.imgstr = ''
			var str = ''
			if (sub.seek_img_str) {
			 	var arr = sub.seek_img_str.split(",");
			 	for (var i = 0; i < arr.length; i++) {
			 		that.uploadImages(arr[i]);
				}
			}
			
			
			

			console.log(sub)
			if (sub.name == '') {
				uni.showToast({
					title: '请输入对方姓名',
					icon: 'none',
					duration: 2000,
					success() { }
				})
				return false;
			}
			if (sub.sex == '') {
				uni.showToast({
					title: '请选择对方性别',
					icon: 'none',
					duration: 2000,
					success() { }
				})
				return false;
			}
			if (sub.age == '') {
				uni.showToast({
					title: '请输入对方年龄',
					icon: 'none',
					duration: 2000,
					success() { }
				})
				return false;
			}
			// if (sub.seek_address == '') {
			// 	uni.showToast({
			// 		title: '请输入所在地址',
			// 		icon: 'none',
			// 		duration: 2000,
			// 		success() { }
			// 	})
			// 	return false;
			// }
			
			if (sub.qu == '') {
				uni.showToast({
					title: '请选择所在地区',
					icon: 'none',
					duration: 2000,
					success() {}
				})
				return false;
			}

			if (that.tabIndex == 0) {
				if (sub.seek_phone == '') {
					uni.showToast({
						title: '请输入我的联系方式',
						icon: 'none',
						duration: 2000,
						success() { }
					})
					return false;
				}
				
				if (sub.seek_type == '') {
					uni.showToast({
						title: '请输入我的需求',
						icon: 'none',
						duration: 2000,
						success() { }
					})
					return false;
				}
			}

			uni.showModal({
				title: '提示：',
				content: '确定提交吗？',
				success: (res) => {
					
					
					str = that.imgstr.replace(/^,+|,+$/g, '');
					
					if (res.confirm) {
						console.log(JSON.parse(JSON.stringify(this.item)))

						this.item.edit_type = "2"
						uni.request({
							url: getApp().globalData.host + 'api/Order/editMyPublish',
							method: 'POST',
							data: {
								edit_type: "2", // 操作类型 1回显信息 2确认编辑
								seek_id: this.id, // 寻人帖子ID
								seek_sex: sub.sex, // 被寻人性别 1男 2女
								seek_name: sub.name, // 被寻人姓名（或寻人方向）
								seek_age: sub.age, // 被寻人年龄
								seek_address: sub.seek_address, // 被寻人地址
								seek_address_two: sub.seek_address_two, // 被寻人地址（备用）
								seek_school: sub.seek_school, // 被寻人学校
								seek_school_two:  sub.seek_school_two, // 被寻人学校（备用）
								seek_company: sub.seek_company, // 被寻人公司
								seek_company_two: sub.seek_company_two, // 被寻人公司（备用）
								seek_img_str: str, // 被寻人图片
								seek_content: sub.seek_content, // 被寻人信息描述
								seek_type: sub.seek_type, // 选择的需求ID
								seek_phone: sub.seek_phone, // 联系方式
								label_id: sub.label_id,
								z_label_id: sub.z_label_id,
								sheng:sub.sheng,
								city:sub.city,
								qu:sub.qu,
								sheng_name:sub.sheng_name,
								city_name:sub.city_name,
								qu_name:sub.qu_name,
							},
							header: {
								memberid: uni.getStorageSync('member_id'),
								membertoken: uni.getStorageSync('member_token'),
							},
							success: (r) => {
								console.log(r.data)
								if (r.data.code != 200) {
									uni.showModal({
										content: r.data.msg
									})
									return false
								}else{
									uni.showToast({
										title: '编辑成功'
									})
									return false
								}
							},
						})
					}
				}
			})
		},
	}
}
</script>
<template>
	<view class="p-30">
		<Header :title="title" />

		<findPerson v-if="Object.keys(item).length > 0" v-show="loading==true" @submit="submit" :editData="item" @listUpdate="listUpdate" :dataArr="dataArr"/>
	</view>
</template>



<style></style>